package com.kermitye.treenodelib

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.CheckBox
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.kermitye.treenodelib.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    val list = arrayListOf<TreeNode<String>>()

    val adapter by lazy { TreeNodeAdapter<String>(this, list) }
    companion object {
        const val ITEM_TYPE_ONE = 0
        const val ITEM_TYPE_TWO = 1
        const val ITEM_TYPE_THREE = 2
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        adapter.addItemViewDelegate(object : TreeNodeDelegate<String>() {

            override val layoutId: Int
                get() = R.layout.item_module

            override fun convert(holder: BaseViewHolder, treeNode: TreeNode<String>) {
                if (holder == null || treeNode == null) return
                holder.itemView.setPadding(0, 0, 0, 0)
                holder.setText(R.id.im_tv_name, treeNode.value)
                holder.getView<CheckBox>(R.id.im_cb)?.apply {
                    isChecked = treeNode.isSelect
                    setOnClickListener {
                        changeSelectTreeNode(treeNode)
                    }
                }
                holder.itemView.setOnClickListener {
                    adapter?.expandOrCollapseTreeNode(treeNode)
                }
            }
            override fun setLevelTag(): Int {
                return ITEM_TYPE_ONE
            }


        })
        adapter.addItemViewDelegate(object : TreeNodeDelegate<String>() {
            init {
                //为1时单选模式
                setMaxSelectCount(1)
            }

            override val layoutId: Int
                get() = R.layout.item_module

            override fun convert(holder: BaseViewHolder, treeNode: TreeNode<String>) {
                if (holder == null || treeNode == null) return
                holder.itemView.setPadding(treeNode.getLevel() * 100, 0, 0, 0)
                holder.setText(R.id.im_tv_name, treeNode.value)
                holder.getView<CheckBox>(R.id.im_cb)?.apply {
                    isChecked = treeNode.isSelect
                    setOnClickListener {
                        changeSelectTreeNode(treeNode)
                    }

                }
                holder.itemView.setOnClickListener {
                    adapter?.expandOrCollapseTreeNode(treeNode)
                }
            }

            override fun setLevelTag(): Int {
                return ITEM_TYPE_TWO
            }

        })
        adapter.addItemViewDelegate(object : TreeNodeDelegate<String>() {

            init {
                setMaxSelectCount(1)
            }

            override val layoutId: Int
                get() = R.layout.item_module

            override fun convert(holder: BaseViewHolder, treeNode: TreeNode<String>) {
                if (holder == null || treeNode == null) return
                holder.itemView.setPadding(treeNode.getLevel() * 100, 0, 0, 0)
                holder.setText(R.id.im_tv_name, treeNode.value)
                holder.getView<CheckBox>(R.id.im_cb)?.apply {
                    isChecked = treeNode.isSelect
                    setOnClickListener {
                        changeSelectTreeNode(treeNode)
                    }
                }
                holder.itemView.setOnClickListener {
                    adapter?.expandOrCollapseTreeNode(treeNode)
                }
            }

            override fun setLevelTag(): Int {
                return ITEM_TYPE_THREE
            }

        })

        binding.rv.layoutManager = LinearLayoutManager(this)
        binding.rv.adapter = adapter
    }

    fun submit(view: View) {
//        adapter.getSelectedData()?.let { printData(it) }
        for (i in 0..9) {
            val treeNode0 = TreeNode("0级:$i")
            list.add(treeNode0)
            for (j in 0..9) {
                val treeNode1 = TreeNode("1级:$j")
                treeNode0.addChild(treeNode1)
                for (k in 0..9) {
                    val treeNode2 = TreeNode("2级:$k", isSelect = k == 2)
                    //                    TreeNode<Integer> treeNode2 = new TreeNode<>(1);
                    treeNode1.addChild(treeNode2)
                }
            }
        }
        adapter.refreshTreeNode()
    }

    fun printData(data: List<TreeNode<String>>) {
        data.forEach {
            if (it.hasChild()) {
                it.children?.let { it1 -> printData(it1) }
            }
            Log.e("TreeNodeLib","parent:${it.parent?.value} = ${it.value}")
        }
    }

}